package com.snow.useviewdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import me.jessyan.autosize.utils.LogUtils;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019/5/13
 * desc   :
 */
public class SongTextView extends View {
    private int postIndex;
    private Paint mPaint;

    private int delta = 15;
    private float mTextHeight;
    private float mTextWidth;
    //        private String mText = "梦 里 面 看 我 七 十 二 变";
    private String mText = "朝辞白帝彩云间，千里江陵一日还。";
    private PorterDuffXfermode xformode;

    public SongTextView(Context ctx) {
        this(ctx, null);
    }

    public SongTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    String[] striingsArray;

    public SongTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        striingsArray = new String[]{"床前明月光，", "疑是地上霜。", "举头望明月，", "低头思故乡。"};
        init();
    }

    public void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        xformode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        mPaint.setColor(Color.CYAN);
        mPaint.setTextSize(60.0f);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setXfermode(null);
        mPaint.setTextAlign(Paint.Align.LEFT);
        //文字精确高度
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        mTextHeight = fontMetrics.bottom - fontMetrics.descent - fontMetrics.ascent;
        mTextWidth = mPaint.measureText(mText);
    }

    /**
     * 计算 控件的宽高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int mWidth;
        final int mHeight;
        /**
         * 设置宽度
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY)// match_parent , accurate
            mWidth = widthSize;
        else {
            // 由图片决定的宽
            int desireByImg = getPaddingLeft() + getPaddingRight()
                    + getMeasuredWidth();
            if (widthMode == MeasureSpec.AT_MOST)// wrap_content
                mWidth = Math.min(desireByImg, widthSize);
            else
                mWidth = desireByImg;
        }
        /***
         * 设置高度
         */
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY)// match_parent , accurate
            mHeight = heightSize;
        else {
            int desire = getPaddingTop() + getPaddingBottom()
                    + getMeasuredHeight();
            if (heightMode == MeasureSpec.AT_MOST)// wrap_content
                mHeight = Math.min(desire, heightSize);
            else
                mHeight = desire;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap srcBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas srcCanvas = new Canvas(srcBitmap);
        srcCanvas.drawText(mText, 0, mTextHeight, mPaint);
        mPaint.setXfermode(xformode);
        mPaint.setColor(Color.RED);
        RectF rectF = new RectF(0, 0, postIndex, getMeasuredHeight());
        srcCanvas.drawRect(rectF, mPaint);
        canvas.drawBitmap(srcBitmap, 0, 0, null);
        init();
        if (postIndex < mTextWidth) {
            postIndex += 10;
        } else {
            postIndex = 0;
//            mhandler.removeMessages(101);
        }
        postInvalidateDelayed(80);

    }
//    private int curLine;
//
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        Bitmap srcBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
//        Canvas srcCanvas = new Canvas(srcBitmap);
//
//        curLine = 0;
//        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
//        float top = fontMetrics.top;
//        float bottom = fontMetrics.bottom;
//        int length = striingsArray.length;
//        float lineHeight = -top + bottom;
//        for (int i = 0; i < length; i++) {
//            curLine++;
//            canvas.drawText(striingsArray[i], 0, curLine * lineHeight, mPaint);
//        }
//
////        srcCanvas.drawText(mText, 0, mTextHeight, mPaint);
//        mPaint.setXfermode(xformode);
//        mPaint.setColor(Color.RED);
//        LogUtils.e("getMeasuredHeight()===" + getMeasuredHeight());
//        RectF rectF = new RectF(0, 0, postIndex, getMeasuredHeight());
//        srcCanvas.drawRect(rectF, mPaint);
//        canvas.drawBitmap(srcBitmap, 0, 0, null);
//        init();
//        LogUtils.e("postIndex < mTextWidth===" + (postIndex < mTextWidth));
//        if (postIndex < mTextWidth) {
//            postIndex += 10;
//        } else {
//            postIndex = 0;
//            mhandler.removeMessages(101);
//        }
//
//    }

//    Handler mhandler = new Handler(new Handler.Callback() {
//        @Override
//        public boolean handleMessage(Message msg) {
//            switch (msg.what) {
//                case 101:
//                    mhandler.sendEmptyMessageDelayed(101, 80);
//                    break;
//            }
//            postInvalidate();
//            return false;
//        }
//    });
//
//    public void draw() {
//        mhandler.sendEmptyMessage(101);
//    }
}
